using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace GUI
{
    public partial class thongtinhocsinh : Form
    {
        public thongtinhocsinh()
        {
            InitializeComponent();
        }
        DataTable dtlop = new DataTable();
        DataTable dthanhkiem = new DataTable();
        DataTable dthocsinh = new DataTable();
        DataTable dtdscon = new DataTable();
        private void thongtinhocsinh_Load(object sender, EventArgs e)
        {
            //hien thi vao combobox lop
            dtlop=hocsinhBUS.danhsachlop();

            cmblop.DataSource = dtlop;
            cmblop.DisplayMember = "tenlop";
            cmblop.ValueMember = "malop";
            cmblop.SelectedIndex = 0;
            
            cmbgioitinh.SelectedIndex = 0;
            radtimma.Enabled = false;
            radtimten.Enabled = false;
            txtnhaptim.Enabled = false;
            btntim.Enabled = false;

            cmbtrangthai.SelectedIndex = 0;
           

            //hien thi danh sach hoc sinh vao listview
            dthocsinh = hocsinhBUS.danhsachhocsinh();
            hienthilistview(listhocsinh, dthocsinh);

            ////an tat ca cac textbox khi load
            //txtmahs.Enabled=false;
            //enable(false);
            
        }
        //hien thi ra listview
        void hienthilistview(ListView lsv, DataTable dt)
        {
            lsv.Items.Clear();
            foreach (DataRow drds in dt.Rows)
                addlistview(drds, lsv);

        }
        // add tung dong vao listview
        void addlistview(DataRow dr, ListView lsv)
        {
            ListViewItem tem = new ListViewItem();
            tem.Text = dr["mahs"].ToString();
            tem.SubItems.Add(dr["hoten"].ToString());
            if ((Boolean)dr["gioitinh"] == true)
                tem.SubItems.Add("Nam");
            else
                tem.SubItems.Add("Nữ");
            tem.SubItems.Add(dr["ngaysinh"].ToString());
            tem.SubItems.Add(dr["diachi"].ToString());
            tem.SubItems.Add(dr["noisinh"].ToString());
            tem.SubItems.Add(dr["malop"].ToString());
            tem.SubItems.Add(dr["tinhtrang"].ToString());
            lsv.Items.Add(tem);
        }

      //hien thi vao tung textbox
        void hienthitext(ListViewItem tem)
        {
            txtmahs.Text = tem.Text;
            txthoten.Text = tem.SubItems[1].Text;
            cmbgioitinh.Text = tem.SubItems[2].Text;
            datengaysinh.Text = tem.SubItems[3].Text;
            txtdiachi.Text = tem.SubItems[4].Text;
            txtnoisinh.Text = tem.SubItems[5].Text;
            cmblop.Text = tem.SubItems[6].Text;
           
            cmbtrangthai.Text = tem.SubItems[7].Text;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //gan gia tri vao hocsinhDTO.
            hocsinhDTO hsdto = new hocsinhDTO();
            hsdto.Mahs = txtmahs.Text;
            hsdto.Hoten = txthoten.Text;
            if (cmbgioitinh.SelectedIndex == 0)
                hsdto.Gioitinh ="1";
                else
                hsdto.Gioitinh = "0";
            //hsdto.Gioitinh = cmbgioitinh.Text;
            hsdto.Ngaysinh = datengaysinh.Text;
            hsdto.Diachi = txtdiachi.Text;
            hsdto.Noisinh = txtnoisinh.Text;
            hsdto.Malop = cmblop.SelectedValue.ToString();
           
            //kiem tra combobox trang thai
            if (cmbtrangthai.SelectedIndex == 1)
                hsdto.Tinhtrang = "Xóa tên";
            else
                if (cmbtrangthai.SelectedIndex == -1)
                    hsdto.Tinhtrang = "Còn học";
            else
                hsdto.Tinhtrang = cmbtrangthai.Text;
        
        
            //kiem tra hanh kiem neu duoc chon thi luu thong tin,khong thi luu rong

        if (radthem.Checked)
        {
            
            if (txtmahs.Text == "" || txthoten.Text == "")
            {
                MessageBox.Show("BẠN PHẢI NHẬP ĐẦY ĐỦ MÃ HOC SINH VÀ TÊN HỌC SINH!", "THÔNG BÁO");
            }
            else
            {
                if (hocsinhBUS.themhocsinh(hsdto) == true)
                {
                    MessageBox.Show("Đã thêm " + hsdto.Hoten + " Thành công!");
                    dthocsinh = hocsinhBUS.danhsachhocsinh();
                    hienthilistview(listhocsinh, dthocsinh);
                }
                else
                {
                    MessageBox.Show("Mã " + hsdto.Mahs + " đã tồn tại!");
                    txtmahs.SelectAll();
                    txtmahs.Focus();
                }
            }
        }
        if (radsua.Checked)
        {
            if (hocsinhBUS.capnhathocsinh(hsdto) == true)
            {
                MessageBox.Show("Đã cập nhật học sinh có mã " + hsdto.Mahs + " Thành công!");
                dthocsinh = hocsinhBUS.danhsachhocsinh();
                hienthilistview(listhocsinh, dthocsinh);
            }
            else
            {
                MessageBox.Show("Mã hoc sinh "+hsdto.Mahs+" không tồn tại, Vui lòng kiểm tra lại!");
                txtmahs.SelectAll();
                txtmahs.Focus();
            }
        }
        if (radxoa.Checked)
        {
            
           
            DialogResult dlg;
            dlg = MessageBox.Show("Bạn muốn xóa?","THÔNG BÁO!",MessageBoxButtons.YesNo);
            if (dlg == DialogResult.Yes)
            {
                if (hocsinhBUS.xoahocsinh(hsdto) == true)
                {
                    MessageBox.Show("Đã xóa học sinh " + hsdto.Hoten + " Thành công!");
                    listhocsinh.Items.Clear();

                    dtdscon = hocsinhBUS.danhsachhocsinhcon();
                    hienthilistview(listhocsinh, dtdscon);
                    lammoi();
                }
                else
                {
                    MessageBox.Show("Xóa không thành công");

                }
            }

        }
        

            
        }
        void enable(Boolean dk)
        {
            txtdiachi.Enabled = dk;
            txthoten.Enabled = dk;
            txtnoisinh.Enabled = dk;
            cmbtrangthai.Enabled = dk; 
            datengaysinh.Enabled = dk;
            cmbtrangthai.Enabled = dk;
            cmbgioitinh.Enabled = dk;
           
            cmblop.Enabled = dk;
        }
        void lammoi()
        {
            txtmahs.Text = "";
            txtdiachi.Text = "";
           
            txthoten.Text = "";
            txtnoisinh.Text = "";
            txtnhaptim.Text = "";
            cmbgioitinh.SelectedIndex = 0;
            
            cmblop.SelectedIndex = 0;
            cmbtrangthai.SelectedIndex = 0;
            datengaysinh.Value = DateTime.Now;

        }

        private void radthem_CheckedChanged(object sender, EventArgs e)
        {
            radtimma.Enabled = false;
            radtimten.Enabled = false;
            txtnhaptim.Enabled = false;
            btntim.Enabled = false;
            lammoi();
            txtmahs.Enabled = true;
            enable(true);
           
        }

        private void radsua_CheckedChanged(object sender, EventArgs e)
        {
            dthocsinh = hocsinhBUS.danhsachhocsinh();
            hienthilistview(listhocsinh, dthocsinh);
            txtnhaptim.Text = "";
            enable(true);
            txtmahs.Enabled = false;
            radtimma.Enabled = false;
            radtimten.Enabled = false;
            txtnhaptim.Enabled = false;
            btntim.Enabled = false;
            
        }

        private void radxoa_CheckedChanged(object sender, EventArgs e)
        {
            dtdscon = hocsinhBUS.danhsachhocsinhcon();
            hienthilistview(listhocsinh, dtdscon);
            txtnhaptim.Text = "";
            radtimma.Enabled = false;
            radtimten.Enabled = false;
            txtnhaptim.Enabled = false;
            btntim.Enabled = false;
            enable(false);
            txtmahs.Enabled = false;
            
        }

        private void radtimkiem_CheckedChanged(object sender, EventArgs e)
        {
            dthocsinh = hocsinhBUS.danhsachhocsinh();
            hienthilistview(listhocsinh, dthocsinh);
            enable(true);
            txtmahs.Enabled = false;
            radtimma.Enabled = true;
            radtimten.Enabled = true;
            txtnhaptim.Enabled = true;
            btntim.Enabled = true;
        }

        private void listhocsinh_SelectedIndexChanged(object sender, EventArgs e)
        {
            //duyet trong danh sach
            if (radthem.Checked)
            {
                lammoi();
            }
            else
            {
                foreach (ListViewItem tm in listhocsinh.SelectedItems)
                    //neu item duoc chon thi hien thi ra textbox
                    if (tm.Selected)
                        hienthitext(tm);
            }
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void btnlammoi_Click(object sender, EventArgs e)
        {
            lammoi();
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            DialogResult dlg;
            dlg = MessageBox.Show("Bạn muốn thoát?","THÔNG BÁO",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
            if (dlg == DialogResult.Yes)
                this.Close();
        }

        private void txtnhaptim_TextChanged(object sender, EventArgs e)
        {
             string chuoitim = txtnhaptim.Text.ToLower();
               foreach (ListViewItem lst in listhocsinh.Items)
                    {
                        lst.Selected = false;
                       listhocsinh.EnsureVisible(0);
                    }
                    if(radtimkiem.Checked)
                        if (radtimma.Checked)
                        {
                            for (int i = 0; i < listhocsinh.Items.Count; i++)
                                if (listhocsinh.Items[i].Text.ToString() == txtnhaptim.Text)
                                {
                                    listhocsinh.Items[i].Selected = true;
                                    listhocsinh.EnsureVisible(i);
                                }
                        }


                
            
        }

        private void btntim_Click(object sender, EventArgs e)
        {
            
            hocsinhDTO hsdto = new hocsinhDTO();
            hsdto.Mahs = txtnhaptim.Text;
            hsdto.Hoten = txtnhaptim.Text;
            if (radtimkiem.Checked)
            {

                if (radtimma.Checked)
                {
                   
                    if(hocsinhBUS.timhocsinhma(hsdto)==true)
                    {
                        //KIEM TRA TRONG LISTVIEW DE HIEN THI LEN TEXTBOX
                        for (int i = 0; i < listhocsinh.Items.Count;i++ )
                        {
                            if (listhocsinh.Items[i].Text.ToString() == hsdto.Mahs)
                            {
                                listhocsinh.Items[i].Selected = true;
                                listhocsinh.Items[i].EnsureVisible();
                                hienthitext(listhocsinh.Items[i]);
                                break;
                            }
                            else

                                listhocsinh.Items[i].Selected = false;
                        }
                    }
                }

                if (radtimten.Checked)
                {

                    if (hocsinhBUS.timhocsinhtheoten(hsdto) == true)
                    {
                        //KIEM TRA TRONG LISTVIEW DE HIEN THI LEN TEXTBOX
                        for (int i = 0; i < listhocsinh.Items.Count; i++)
                        {
                            if (listhocsinh.Items[i].SubItems[1].Text.ToString() == hsdto.Hoten)
                            {
                                listhocsinh.Items[i].Selected = true;
                                listhocsinh.Items[i].EnsureVisible();
                                hienthitext(listhocsinh.Items[i]);
                                break;
                            }
                            else

                                listhocsinh.Items[i].Selected = false;
                        }
                    }
                }
            }
            
        }

        private void radtimma_CheckedChanged(object sender, EventArgs e)
        {
            txtnhaptim.Text = "";
        }

        private void radtimten_CheckedChanged(object sender, EventArgs e)
        {
            txtnhaptim.Text = "";
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }


    }
}